/*********************************************************************
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2017  SEGGER Microcontroller GmbH & Co. KG       *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V5.44 - Graphical user interface for embedded applications **
All  Intellectual Property rights  in the Software belongs to  SEGGER.
emWin is protected by  international copyright laws.  Knowledge of the
source code may not be used to write a similar product.  This file may
only be used in accordance with the following terms:

The  software has  been licensed  to STMicroelectronics International
N.V. a Dutch company with a Swiss branch and its headquarters in Plan-
les-Ouates, Geneva, 39 Chemin du Champ des Filles, Switzerland for the
purposes of creating libraries for ARM Cortex-M-based 32-bit microcon_
troller products commercialized by Licensee only, sublicensed and dis_
tributed under the terms and conditions of the End User License Agree_
ment supplied by STMicroelectronics International N.V.
Full source code is available at: www.segger.com

We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_X_OS.C
Purpose     : This file provides emWin Interface with FreeRTOS
---------------------------END-OF-HEADER------------------------------
*/

/**
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics. 
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license SLA0044,
  * the "License"; You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *                      http://www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

#include "GUI.h"

/*********************************************************************
*
* Global data
*/
static rt_mutex_t osMutex = RT_NULL;
static rt_sem_t osSemaphore = RT_NULL;
static rt_sem_t KeySem = RT_NULL;
static int KeyPressed;
static char KeyIsInited = RT_FALSE;
/*********************************************************************
*
* Timing:
* GUI_X_GetTime()
* GUI_X_Delay(int)

Some timing dependent routines require a GetTime
and delay function. Default time unit (tick), normally is
1 ms.
*/

int GUI_X_GetTime(void)
{
    return ((int)rt_tick_get());
}

void GUI_X_Delay(int ms)
{
    rt_thread_mdelay(ms);
}

/*********************************************************************
*
* GUI_X_ExecIdle
*
* Note:
* Called if WM is in idle state
*/

void GUI_X_ExecIdle(void)
{
    GUI_X_Delay(1);
}

/*********************************************************************
*
* Multitasking:
*
* GUI_X_InitOS()
* GUI_X_GetTaskId()
* GUI_X_Lock()
* GUI_X_Unlock()
*
* Note:
* The following routines are required only if emWin is used in a
* true multi task environment, which means you have more than one
* thread using the emWin API.
* In this case the
* #define GUI_OS 1
* needs to be in GUIConf.h
*/

/* Init OS */
void GUI_X_InitOS(void)
{

    /* Create the Mutex used by the two threads */
    osMutex = rt_mutex_create("STemWin", RT_IPC_FLAG_FIFO);

    /* Create the Semaphore used by the two threads */
    osSemaphore = rt_sem_create("STemWin", 1, RT_IPC_FLAG_FIFO);
}

void GUI_X_Unlock(void)
{
    rt_mutex_release(osMutex);
}

void GUI_X_Lock(void)
{
    rt_mutex_take(osMutex, RT_WAITING_FOREVER);
}

/* Get Task handle */
U32 GUI_X_GetTaskId(void)
{
    rt_uint8_t id = rt_thread_self()->current_priority;
    return ((U32)id);
}

void GUI_X_WaitEvent(void)
{
    rt_sem_take(osSemaphore, RT_WAITING_FOREVER);
}

void GUI_X_SignalEvent(void)
{
    rt_sem_release(osSemaphore);
}

/*
*********************************************************************************************************
* KEYBOARD INTERFACE FUNCTIONS
*
* Purpose: The keyboard routines are required only by some widgets.
* If widgets are not used, they may be eliminated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
* CheckInit()
*
* Description : Initialize the GUI keyboard if it is not already done.
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : GUI_X_WaitKey().
* GUI_X_GetKey().
*
* Note(s) : none.
*********************************************************************************************************
*/
void CheckInit(void)
{
    if (KeyIsInited == RT_FALSE)
    {
        KeyIsInited = RT_TRUE;
        GUI_X_Init();
    }
}

/*********************************************************************
*
* GUI_X_Init()
*
* Note:
* GUI_X_Init() is called from GUI_Init is a possibility to init
* some hardware which needs to be up and running before the GUI.
* If not required, leave this routine blank.
*/

void GUI_X_Init(void)
{
    KeySem = rt_sem_create("KeySem", 0, RT_IPC_FLAG_FIFO);
}

/*
*********************************************************************************************************
* GUI_X_GetKey()
*
* Description : Get the pressed key.
*
* Argument(s) : none.
*
* Return(s) : Pressed key.
*
* Caller(s) : various.
*
* Note(s) : none.
*********************************************************************************************************
*/
int GUI_X_GetKey(void)
{
    int r;
    r = KeyPressed;
    CheckInit();
    KeyPressed = 0;
    return (r);
}

/*
*********************************************************************************************************
* GUI_X_WaitKey()
*
* Description : Wait for a key to be pressed and return it.
*
* Argument(s) : none.
*
* Return(s) : Pressed key.
*
* Caller(s) : various.
*
* Note(s) : none.
*********************************************************************************************************
*/
int GUI_X_WaitKey(void)
{
    int r;
    CheckInit();
    if (KeyPressed == 0)
    {
        rt_sem_take(KeySem, RT_WAITING_FOREVER);
    }
    r = KeyPressed;
    KeyPressed = 0;
    return (r);
}

/*
*********************************************************************************************************
* GUI_X_StoreKey()
*
* Description : Store the pressed key.
*
* Argument(s) : Pressed key.
*
* Return(s) : none.
*
* Caller(s) : various.
*
* Note(s) : none.
*********************************************************************************************************
*/
void GUI_X_StoreKey(int k) //--------------（ 14）
{
    KeyPressed = k;
    rt_sem_release(KeySem);
}

/*********************************************************************
*
* Logging: OS dependent

Note:
Logging is used in higher debug levels only. The typical target
build does not use logging and does therefor not require any of
the logging routines below. For a release build without logging
the routines below may be eliminated to save some space.
(If the linker is not function aware and eliminates unreferenced
functions automatically)

*/

void GUI_X_Log(const char *s) {}
void GUI_X_Warn(const char *s) {}
void GUI_X_ErrorOut(const char *s) {}

/*************************** End of file ****************************/
